package com.example.gaojt.androiddemo;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.TextView;

/**
 * Created by gaojt on 8/7/17.
 */

public class TabStyleActivity extends Activity {
    SensorManager mSensorManager;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_style_ly);
        TextView textView = (TextView) findViewById(R.id.text11);
        textView.setSelected(true);

        mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        /*通过SensorManager获取相应的（温度传感器）Sensor类型对象*/
        Sensor mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
        Log.d("aac", "onCreate: mSensor = " + mSensor);
        mSensorManager.registerListener(mSensorEventListener, mSensor
                , SensorManager.SENSOR_DELAY_NORMAL);
    }

    private final SensorEventListener mSensorEventListener=new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            if(event.sensor.getType()==Sensor.TYPE_AMBIENT_TEMPERATURE){
                /*温度传感器返回当前的温度，单位是摄氏度（°C）。*/
                float temperature=event.values[0];
                Log.d("aac", "onSensorChanged: temperature = " + temperature);
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSensorManager.unregisterListener(mSensorEventListener);
    }
}
